﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.Common;
using SLLottery.LotterySrv;
namespace SLLottery
{
    public partial class ChangeBonusWindow : ChildWindow
    {
        private bool isSysSet = false;
        private LotteryServiceClient srv;
        public ChangeBonusWindow()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            sliderBonus.ValueChanged += new RoutedPropertyChangedEventHandler<double>(sliderBonus_ValueChanged);
            this.Loaded += new RoutedEventHandler(ChangeBonusWindow_Loaded);
            
        }
        private double BonusPct = 0;
        private double Scale = 0;
        private double Bonus = 0;
        private int Multiple = 0;
        private LotteryType lotteryType = LotteryType.SSC;
        private int PlayTypeID = 1;
        public ChangeBonusWindow(double bonusPct,double scale,double bonus,int multiple,LotteryType type,int playtype)
            : this()
        {
            BonusPct = bonusPct;
            Scale = scale;
            Bonus = bonus;
            Multiple = multiple;
            lotteryType = type;
            PlayTypeID = playtype;
        }
        void ChangeBonusWindow_Loaded(object sender, RoutedEventArgs e)
        {
            SetBonus();
        }
        void SetBonus()
        {
            Constant.MaxScale = BonusCalc.GetMaxScale();
            Constant.MinScale = BonusCalc.GetMinScale();
            Constant.MaxBonusPct = BonusCalc.GetMaxBonusPct();
            sliderBonus.Minimum = 0;
            sliderBonus.Maximum = Constant.MaxBonusPct;
            //sliderBonus.LargeChange = 0.5;
            sliderBonus.IsDirectionReversed = true;
            sliderBonus.Value = BonusPct;

            List<string> result = new List<string>();
            string Bonus = string.Empty;
            string Scale = string.Empty;
            Bonus = BonusCalc.GetBonus(sliderBonus.Value);
            Scale = BonusCalc.GetScale(sliderBonus.Value);
            result.Add(Bonus);
            result.Add(Scale);
            result.Add(sliderBonus.Value.ToString("F2") + "%");
            this.Tag = result;
            this.lbBackPct.Text = sliderBonus.Value.ToString("F2") + "%";
            lbBonus.Text = Scale + " / " + Bonus;

        }

        void sliderBonus_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (isSysSet)
                return;
            isSysSet = true;
            sliderBonus.Value = GetSliderValue(e.NewValue);
            List<string> result = new List<string>();
            string Bonus = string.Empty;
            string Scale = string.Empty;
            Bonus = BonusCalc.GetBonus(sliderBonus.Value);
            Scale = BonusCalc.GetScale(sliderBonus.Value);
            result.Add(Bonus);
            result.Add(Scale);
            result.Add(sliderBonus.Value.ToString("F2") + "%");
            this.Tag = result;
            this.lbBackPct.Text = sliderBonus.Value.ToString("F2") + "%";
            lbBonus.Text = Scale + " / " + Bonus;
            isSysSet = false;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            BonusInfo bonus = new BonusInfo();
            bonus.BackPct = sliderBonus.Value;
            bonus.PlayTypeID = PlayTypeID;
            bonus.UserID = App.Session[Constant.USERNAME_SESSION].ToString();
            bonus.LotteryTypeID = (int)lotteryType;
            bonus.PlayTypeRadioID = MainPage.playTypeRadio;
            if (bonus.BackPct == BonusPct)
            { }
            else
            {
                gridMain.Visibility = Visibility.Collapsed;
                gridLoading.Visibility = Visibility.Visible;
                srv.UpdateBonusAsync(bonus);
                srv.UpdateBonusCompleted += new EventHandler<UpdateBonusCompletedEventArgs>(srv_UpdateBonusCompleted);
            }
            //this.DialogResult = true;
        }

        void srv_UpdateBonusCompleted(object sender, UpdateBonusCompletedEventArgs e)
        {
            gridLoading.Visibility = Visibility.Collapsed;
            if (e.Result)
            {
                gridComplete.Visibility = Visibility.Visible;
                lbMsg.Text = "修改成功";
            }
            else
            {
                Messager.Show("系统异常，请联系客服");
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
        public  double GetSliderValue(double Value)
        {
            User user = App.Session[Constant.USERINFO_SESSION] as User;
            if ((BonusPct % 0.5) != 0 || (user.Bonus % 0.5) != 0)
            {
                return Math.Round(Value, 1);
            }
            else
            {
               if (Math.Round(Value, 1) > (Math.Floor(Value) + 0.5))
               {
                   return Math.Ceiling(Value);
               }
               else if (Math.Round(Value, 1) != (Math.Floor(Value)))
               {
                   return Math.Floor(Value) + 0.50;
               }
               else if (Math.Round(Value, 1) == (Math.Floor(Value)))
               {
                   return Math.Floor(Value);
               }
               else
                   return Value;
            }
        }
        //add
        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.sliderBonus.Value >= Constant.MaxBonusPct)
                return;
            User user = App.Session[Constant.USERINFO_SESSION] as User;
            if ((BonusPct % 0.5) != 0 || (user.Bonus % 0.5) != 0)
            {
                this.sliderBonus.Value = this.sliderBonus.Value + 0.1;
            }
            else
            {
                this.sliderBonus.Value = this.sliderBonus.Value + 0.5;
            }
        }
        //sub
        private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            if (this.sliderBonus.Value < 0.1)
                return;
            User user = App.Session[Constant.USERINFO_SESSION] as User;
            if ((BonusPct % 0.5) != 0 || (user.Bonus % 0.5) != 0)
            {
                this.sliderBonus.Value = this.sliderBonus.Value - 0.1;
            }
            else
            {
                this.sliderBonus.Value = this.sliderBonus.Value - 0.5;
            }
        }
    }
}

